/**
 * LocationAPITools library, version 1.0.2
 * 
 * WWW: http://code.google.com/p/locationapitools
 *   
 * Summary:
 *   A collection of classes that simplify the development of location-aware mobile
 *   applications using the JSR-179 Location API on the J2ME (CLDC/MIDP) platform,
 *   e.g. on GPS-equipped smartphones.
 * 
 * Author:
 *   Matthias Stevens (Sony CSL Paris / Vrije Universiteit Brussel)
 *   Contact: matthias.stevens@gmail.com
 *  
 * Description:
 *   The primary use case is a location tracker that stores tracks in KML
 *   (cfr. Google Earth/Maps), CSV or plain text format.
 * 
 *   Package "com.sony.csl.j2me.locationapitools.demo" contains a MIDlet class that
 *   demonstrates how LocationAPITools can be used to create simple GPS tracker
 *   application that produces KML files. For examples of more advanced uses of this
 *   library refer to the NoiseTube project (see below, look for "NoiseTube MobileNoiseTagger").
 * 
 *   The code is known to be working on the following Symbian OS-based platforms:
 *     * S60 3rd Edition Feature Pack 1 (Symbian OS 9.2), e.g.: Nokia N95 and Nokia N95 8GB smartphones
 *     * S60 3rd Edition Feature Pack 2 (Symbian OS 9.3) 
 *   Although this is untested, the code should also work on other (non-Symbian/S60)
 *   J2ME CLDC/MIDP platforms/devices, as long as the JSR-179 Location API is supported
 *   on the platform/device in question.
 * 
 *   This library was developed within the scope of the NoiseTube project at
 *   Sony Computer Science Laboratory (CSL) Paris, for more information please refer to:
 *     * http://noisetube.net
 *     * http://code.google.com/p/noisetube
 *     * http://www.csl.sony.fr 
 *    
 * License: 
 *   Copyright 2008-2009 Sony CSL Paris
 *   Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 *   this file except in compliance with the License. You may obtain a copy of the
 *   License at http://www.apache.org/licenses/LICENSE-2.0.
 *   Unless required by applicable law or agreed to in writing, software distributed
 *   under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *   CONDITIONS OF ANY KIND, either express or implied. See the License for the
 *   specific language governing permissions and limitations under the License.
 */

package com.sony.csl.j2me.locationapitools.tracking;

import javax.microedition.location.Location;
import javax.microedition.location.LocationProvider;

import com.sony.csl.j2me.locationapitools.*;
import com.sony.csl.j2me.util.CustomStringBuffer;
import com.sony.csl.j2me.util.StringUtils;


/**
 * A TrackWriter subclass that stores a location track as a plain text (.txt) file
 * 
 * @author mstevens (Sony CSL Paris / Vrije Universiteit Brussel)
 *
 */
public class TXTTrackWriter extends TrackWriter
{
	
	public TXTTrackWriter(LocationProxy locationProxy, String trackFileFolderPath, String trackName) throws Exception
	{
		this(locationProxy, trackFileFolderPath, trackName, false);
	}

	public TXTTrackWriter(LocationProxy locationProxy, String trackFileFolderPath, String trackName, boolean tracksInvalids) throws Exception
	{
		super(locationProxy, trackFileFolderPath, trackName, tracksInvalids);
	}

	/**
	 * @see com.sony.csl.j2me.locationapitools.tracking.TrackWriter#getFileExtension()
	 */
	protected String getFileExtension()
	{
		return "txt";
	}

	/**
	 * @see com.sony.csl.j2me.locationapitools.tracking.TrackWriter#writeHeader()
	 */
	protected void writeHeader()
	{
		CustomStringBuffer bff = new CustomStringBuffer();
		bff.appendLine("Track named \"" + trackName + "\"");
		bff.appendLine("Created with MobileNoiseTagger on " + StringUtils.formatDateTime(System.currentTimeMillis(), "/", ":", " ") + ".");
		bff.appendLine("Coordinate tuples (NUMBER | TIME | LONGITUDE | LATITUDE | ALTITUDE):");
		//Write to file
		writeToFile(bff.toString());
	}

	/**
	 * @see com.sony.csl.j2me.locationapitools.tracking.TrackWriter#writeLocation(javax.microedition.location.Location)
	 */
	protected void writeLocation(Location location)
	{
		CustomStringBuffer bff = new CustomStringBuffer();
		bff.append("\t" + numberOfLocations + " | " + StringUtils.formatDateTime(System.currentTimeMillis(), "/", ":", " ") + " | ");
		if(location.isValid())
			bff.appendLine(LocationAPIUtilities.coordinatesTuple(location.getQualifiedCoordinates(), true, " | "));
		else
			bff.appendLine("Invalid location!");
		//Write to file
		writeToFile(bff.toString());
	}

	/**
	 * @see com.sony.csl.j2me.locationapitools.tracking.TrackWriter#writePaused(java.lang.String)
	 */
	protected void writePaused(String reason)
	{
		writeToFile("Tracking paused at " + StringUtils.formatDateTime(System.currentTimeMillis(), "/", ":", " ") + ((reason != null) ? (" reason: " + reason) : ""));		
	}
	
	/**
	 * @see com.sony.csl.j2me.locationapitools.tracking.TrackWriter#writeResumed()
	 */
	protected void writeResumed()
	{
		writeToFile("Tracking resumed at " + StringUtils.formatDateTime(System.currentTimeMillis(), "/", ":", " "));		
	}

	/**
	 * @see com.sony.csl.j2me.locationapitools.tracking.TrackWriter#writeProviderStateChanged(javax.microedition.location.LocationProvider, int)
	 */
	protected void writeProviderStateChanged(LocationProvider provider, int newState)
	{
		writeToFile("LocationProvider state changed to " + LocationAPIUtilities.locationProviderState(newState) + " at " + StringUtils.formatDateTime(System.currentTimeMillis(), "/", ":", " "));
	}
	
	/**
	 * @see com.sony.csl.j2me.locationapitools.tracking.TrackWriter#writeFooter()
	 */
	protected void writeFooter()
	{
		CustomStringBuffer bff = new CustomStringBuffer();
		bff.appendLine("Total number of locations: " + numberOfLocations);
		//Write to file
		writeToFile(bff.toString());
	}
	
	/**
	 * @see com.sony.csl.j2me.locationapitools.tracking.TrackWriter#initializeBeforeStart()
	 */
	protected void initializeBeforeStart() { }

}
